/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.editor.editparts;

import org.apache.pig.pigpen.editor.model.Link;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;

public class LinkEditPart extends AbstractConnectionEditPart {

    public LinkEditPart() {
        super();
        setModel(new Link());
    }

    public LinkEditPart(Link model) {
        super();
        setModel(model);
    }

    protected void createEditPolicies() {

    }

    protected void refreshVisuals() {
        PolylineConnection figure = (PolylineConnection) getFigure();
        figure.setLineWidth(2);

        // add arrow at end of line:
        PolygonDecoration decoration = new PolygonDecoration();
        PointList decorationPointList = new PointList();
        decorationPointList.addPoint(0, 0);
        decorationPointList.addPoint(-1, 1);
        decorationPointList.addPoint(-1, -1);
        decoration.setTemplate(decorationPointList);
        figure.setTargetDecoration(decoration);

        Link conn = (Link) getModel();
        OperatorEditPart source = conn.getSource().getEditPart();
        OperatorEditPart target = conn.getTarget().getEditPart();

        if (source != null && target != null) {
            figure.setSourceAnchor(source.getSourceConnectionAnchor(this));
            figure.setTargetAnchor(target.getTargetConnectionAnchor(this));
        }
    }

    public Link getModel() {
        return (Link) super.getModel();
    }

    public String toString() {
        return "LinkEditPart( " + getModel() + " )";
    }

}